﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using TFSTools.TFSLibrary;

namespace TFSTools.TestManagement.TestCaseReview
{
    public class WorkItemTestDataContext : INotifyPropertyChanged
    {
        private TFSWorkItem workItem;
        private string[] userNames;

        public WorkItemTestDataContext(TFSWorkItem workItem)
        {
            if (this.workItem == null)
            {
                throw new ArgumentException("workItem cannot be null");
            }

            this.workItem = workItem;

            this.userNames = (from identity in workItem.Connection.GroupSecurity.GetUserIdentities()
                          select identity.DisplayName).ToArray();
        }

        public string State
        {
            get { return this.workItem.State; }
            set
            {
                this.workItem.State = value;
                this.OnPropertyChanged("Reason");
                this.OnPropertyChanged("AvailableReasons");
                this.OnPropertyChanged("IsStateChanged");
                this.OnPropertyChanged("IsStateTheSame");
            }
        }

        public string[] AvailableStates { get { return this.workItem.AvailableStates; } }

        public string Reason
        {
            get { return this.workItem.Reason; }
            set
            {
                this.workItem.Reason = value;
                this.OnPropertyChanged("Reason");
            }
        }

        public string[] AvailableReasons { get { return this.workItem.AvailableReasons; } }

        public bool IsStateChanged { get { return this.workItem.IsStateChanged; } }

        public bool IsStateTheSame { get { return !this.workItem.IsStateChanged; } }

        public string AssignedTo
        {
            get { return this.workItem.Fields["Assigned To"].ValueString; }
            set
            {
                this.workItem.Fields["Assigned To"].Value = value;
                this.OnPropertyChanged("AssignedTo");
            }
        }

        public string[] UserNames { get { return this.userNames; } }

        #region OnPropertyChanged

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
